package com.shop2cn.iapi.sqdistributor.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.BindMerchantRootDistributorReq;
import com.shop2cn.iapi.sqdistributor.service.BindMerchantRootDistributorCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.DGSQBaseService;
import com.ymttest.database.model.sqdistributor.DistributorRelation;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorRelationWrapper;
import com.ymttest.utils.TestDataManager;

public class Ts_BindMerchantRootDistributor {
	
	//java接口
	private static BindMerchantRootDistributorReq bindMerchantRootDistributorReq;
	private static BindMerchantRootDistributorCall bindMerchantRootDistributorCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("绑定商户的顶级分销员");
	}

	@Before
	public void caseUp() {
		bindMerchantRootDistributorReq = new BindMerchantRootDistributorReq();
		bindMerchantRootDistributorCall = new BindMerchantRootDistributorCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(bindMerchantRootDistributorCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb() throws Exception {
		
		DistributorRelationWrapper distributorRelationWrapper = new DistributorRelationWrapper();

		DistributorRelation actualResult = distributorRelationWrapper
				.selectByMerchantId(bindMerchantRootDistributorReq
						.getMerchantId()).get(0);
		
		Logger.verifyEquals(
				bindMerchantRootDistributorReq.getMerchantName(),
				actualResult.getDistributor_name(), "验证Distributor_name");
		Logger.verifyEquals(
				bindMerchantRootDistributorReq.getMerchantPhone(),
				actualResult.getDistributor_phone(), "验证Distributor_phone");
		Logger.verifyEquals(0, actualResult.getDistributor_level_index(),
				"验证Distributor_level_index");
		Logger.verifyEquals(0, actualResult.getDistributor_status(),
				"验证Distributor_status");
		Logger.verifyEquals(0L, actualResult.getParent_distributor_id(),
				"验证Parent_distributor_id");
		Logger.verifyEquals(0, actualResult.getInvite_count(),
				"验证Invite_count");
		Logger.verifyEquals(0, actualResult.getDirect_subordinate_count(),
				"验证Direct_subordinate_count");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMerchantRootDistributor_001() {
		Logger.start(true, "神器商户开通顶级分销员成功");
		
		try {
			
			int merchantId = DGSQBaseService.createGoodsOwner("USA");
			
			bindMerchantRootDistributorReq.setMerchantName(TestDataManager.getChineseName());
			bindMerchantRootDistributorReq.setMerchantPhone(TestDataManager.getPhoneNum());
			bindMerchantRootDistributorReq.setMerchantId((long)merchantId);
			
			bindMerchantRootDistributorCall.setData(bindMerchantRootDistributorReq);
			bindMerchantRootDistributorCall.callService();
			
			Logger.verifyEquals(true, bindMerchantRootDistributorCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMerchantRootDistributor_002() {
		Logger.start(true, "神器商户重复开通顶级分销员成功");
		
		try {
			
			int merchantId = DGSQBaseService.createGoodsOwner("USA");
			
			bindMerchantRootDistributorReq.setMerchantName(TestDataManager.getChineseName());
			bindMerchantRootDistributorReq.setMerchantPhone(TestDataManager.getPhoneNum());
			bindMerchantRootDistributorReq.setMerchantId((long)merchantId);
			
			bindMerchantRootDistributorCall.setData(bindMerchantRootDistributorReq);
			bindMerchantRootDistributorCall.callService();
			
			// 再次开通，使用第一次开通的数据
			
			BindMerchantRootDistributorReq newReq = new BindMerchantRootDistributorReq();
			
			newReq.setMerchantName(TestDataManager.getChineseName());
			newReq.setMerchantPhone(TestDataManager.getPhoneNum());
			newReq.setMerchantId((long)merchantId);
			
			bindMerchantRootDistributorCall.setData(newReq);
			bindMerchantRootDistributorCall.callService();
			
			Logger.verifyEquals(true, bindMerchantRootDistributorCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMerchantRootDistributor_004() {
		Logger.start(true, "DistributorName为null，开通顶级分销员失败");
		
		try {
			
			//bindMerchantRootDistributorReq.setMerchantName(TestDataManager.getChineseName());
			bindMerchantRootDistributorReq.setMerchantPhone(TestDataManager.getPhoneNum());
			bindMerchantRootDistributorReq.setMerchantId((long)TestDataManager.getRandomIntNum(5));
			
			bindMerchantRootDistributorCall.setData(bindMerchantRootDistributorReq);
			bindMerchantRootDistributorCall.callService();
			
			Logger.verifyEquals(false,
					bindMerchantRootDistributorCall.getSuccess(), "验证success");
			Logger.verifyEquals("merchantName:must not be empty",
					bindMerchantRootDistributorCall.getMsg(), "验证resultMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMerchantRootDistributor_005() {
		Logger.start(true, "DistributorPhone为null，开通顶级分销员失败");
		
		try {
			
			bindMerchantRootDistributorReq.setMerchantName(TestDataManager.getChineseName());
			//bindMerchantRootDistributorReq.setMerchantPhone(TestDataManager.getPhoneNum());
			bindMerchantRootDistributorReq.setMerchantId((long)TestDataManager.getRandomIntNum(5));
			
			bindMerchantRootDistributorCall.setData(bindMerchantRootDistributorReq);
			bindMerchantRootDistributorCall.callService();
			
			Logger.verifyEquals(false,
					bindMerchantRootDistributorCall.getSuccess(), "验证success");
			Logger.verifyEquals("merchantPhone:must not be empty",
					bindMerchantRootDistributorCall.getMsg(), "验证resultMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindMerchantRootDistributor_006() {
		Logger.start(true, "MerchantId为null，开通顶级分销员失败");
		
		try {
			
			bindMerchantRootDistributorReq.setMerchantName(TestDataManager.getChineseName());
			bindMerchantRootDistributorReq.setMerchantPhone(TestDataManager.getPhoneNum());
			//bindMerchantRootDistributorReq.setMerchantId((long)TestDataManager.getRandomIntNum(5));
			
			bindMerchantRootDistributorCall.setData(bindMerchantRootDistributorReq);
			bindMerchantRootDistributorCall.callService();
			
			Logger.verifyEquals(false,
					bindMerchantRootDistributorCall.getSuccess(), "验证success");
			Logger.verifyEquals("merchantId:must be greater than or equal to 1",
					bindMerchantRootDistributorCall.getMsg(), "验证resultMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
